package com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hyphenate.easeui.utils.map.BitmapViewUtils;
import com.hyphenate.easeui.utils.map.JSONUtils;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.Parameter;
import com.kongzue.dialog.v3.WaitDialog;
import com.ninetaildemonfox.zdl.socialproject.base.BaseActivity;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.data.entity.Event2Message;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.ShangCContract;
import com.ninetaildemonfox.zdl.socialproject.utils.GlideUtils;
import com.ninetaildemonfox.zdl.socialproject.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.io.File;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jzvd.JZVideoPlayer;
import cn.jzvd.JZVideoPlayerStandard;

/**
 * @author NineTailDemonFox
 * @date 2019/8/23 11:33
 * 功能描述：图片视频上传
 * 联系方式：1037438704@qq.com
 */

public class PictureUploadActivity extends BaseActivity {
    @BindView(R.id.image_left_finish)
    ImageView imageleftFinish;
    @BindView(R.id.videoplayer)
    JZVideoPlayerStandard videoplayer;
    @BindView(R.id.text_center)
    TextView textCenter;
    @BindView(R.id.text_right)
    TextView textRight;
    @BindView(R.id.text_burn_after_reading)
    TextView textBurnAfterReading;

    @BindView(R.id.aaaaaaa)
    ImageView aaaaaaa;
    private File file;
    private File fileVideo;
    private String type = "", file_type = "";
    private Drawable drawable;
    private String is_see = "1";
    private String picId = "", path = "";

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_picture_upload;
    }


    @Subscribe
    @Override
    public void onInitialization(Bundle bundle) {
        EventBus.getDefault().register(this);
        textCenter.setVisibility(View.GONE);
        imageleftFinish.setVisibility(View.VISIBLE);
        textRight.setVisibility(View.VISIBLE);
        textRight.setText("确定");
        file_type = getIntent().getExtras().getString("file_type");
        type = getIntent().getExtras().getString("type");
        if (getIntent().getExtras().getString("picId") != null) {
            picId = getIntent().getExtras().getString("picId");
        }

        if (getIntent().getExtras().getString("path") != null) {
            path = getIntent().getExtras().getString("path");
            Log.d("File", "=========path===========" + path);
            if (file_type.equals("1")) {
                aaaaaaa.setVisibility(View.GONE);
                videoplayer.setVisibility(View.VISIBLE);
                //视频
                fileVideo = new File(path);
                MediaMetadataRetriever media = new MediaMetadataRetriever();
                media.setDataSource(path);
                Bitmap bitmap = media.getFrameAtTime();
                String bitmapurl = BitmapViewUtils.saveBitmap(me, bitmap);
                file = new File(bitmapurl);
                videoplayer.setUp(path,
                        JZVideoPlayerStandard.SCREEN_WINDOW_NORMAL,
                        "");
                videoplayer.thumbImageView.setImageBitmap(bitmap);
            } else {
                //图片
                videoplayer.setVisibility(View.GONE);
                aaaaaaa.setVisibility(View.VISIBLE);
                GlideUtils.originalGraph(me, getIntent().getExtras().getString("path"), aaaaaaa);
                file = new File(path);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onBackPressed() {
        if (JZVideoPlayer.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        JZVideoPlayer.releaseAllVideos();
    }

    @OnClick({R.id.image_left_finish, R.id.text_right, R.id.text_burn_after_reading})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.image_left_finish:
                finish();
                break;
            case R.id.text_right:
                EventBus.getDefault().post(new Event2Message(file, is_see, type, picId, file_type, path));
                finish();
                break;
            case R.id.text_burn_after_reading:
                if (is_see.equals("2")) {
                    drawable = getResources().getDrawable(R.mipmap.ic_circular_false);
                    is_see = "1";
                } else {
                    is_see = "2";
                    drawable = getResources().getDrawable(R.mipmap.ic_circular_true);
                }
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                textBurnAfterReading.setCompoundDrawables(drawable, null, null, null);
                break;
            default:
        }
    }

    public static void start(Context me, String url, String type, String file_type, String picId) {
        Intent intent = new Intent(me, PictureUploadActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("path", url);
        bundle.putString("type", type);
        bundle.putString("file_type", file_type);
        bundle.putString("picId", picId);
        intent.putExtras(bundle);
        me.startActivity(intent);
    }

    // 上传图片      1上传 2设置       1视频 2 图片
    public static void start(Context me, String path, String type, String file_type) {
        Intent intent = new Intent(me, PictureUploadActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("path", path);
        bundle.putString("type", type);
        bundle.putString("file_type", file_type);
        intent.putExtras(bundle);
        me.startActivity(intent);
    }
}